# -*- coding: utf-8 -*-
import os
import LogUtil as L
from ConfigUtil import ConfigIni
from AdbUtils import ADB
import random
import subprocess
import time

def getscreencap(filename,path):
    '''
    默认采用minicap截图
    :param filename: 保存图片文件名
    :param path:图片保存路径
    :return:
    '''
    import subprocess
    import time
    begin = time.time()
    pi = subprocess.Popen(
        'adb shell "LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/minicap -P 1920x1080@1920x1080/0 -s >/data/local/tmp/{}" '.format(filename),
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    for line in pi.stdout.readlines():
        # print(i.strip().decode('utf-8'))
        if 'Using' in line.strip().decode('utf-8'):
            p2 = subprocess.Popen("adb pull /data/local/tmp/{} {}".format(filename,path),
                                  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            for line2 in p2.stdout.readlines():
                if 'pulled' in line2.strip().decode('utf-8'):
                    subprocess.Popen("adb shell rm /data/local/tmp/{}".format(filename), stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
                    end = time.time()
                    totaltime=end - begin
                    time1=str(totaltime)
                    # print('minicap截图完成，耗时{}秒'.format(time1))
                    time.sleep(5)
                    return


def get_screencap_by_system(filename,path):
    '''
    默认采用minicap截图
    :param filename: 保存图片文件名
    :param path:图片保存路径
    :return:
    '''
    import subprocess
    import time
    begin = time.time()
    pi = subprocess.run("adb shell screencap -p /data/local/tmp/{}".format(filename))
    time.sleep(1)
    p2 = subprocess.Popen("adb pull /data/local/tmp/{} {}".format(filename, path),
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    for line2 in p2.stdout.readlines():
        if 'pulled' in line2.strip().decode('utf-8'):
            subprocess.Popen("adb shell rm /data/local/tmp/{}".format(filename), stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
            end = time.time()
            totaltime = end - begin
            time1 = str(totaltime)
            # print('system截图完成，耗时{}秒'.format(time1))
            return


def get_screencap_by_natvie(path):
    '''
    模拟硬件截图
    :return:
    '''

    begin = time.time()
    p=subprocess.Popen("adb shell input keyevent 120",shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    time.sleep(4)
    # print(pi.returncode)
    p2 = subprocess.Popen("adb pull /sdcard/pictures/Screenshots/ {}".format(path),
                          stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    for line2 in p2.stdout.readlines():
        if 'pulled' in line2.strip().decode('utf-8'):
            end = time.time()
            totaltime = end - begin
            time1 = str(totaltime)
            print('按键截图完成，耗时{}秒'.format(time1))
            subprocess.Popen("adb shell rm /sdcard/pictures/Screenshots/*.png", stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
            return

if __name__ == '__main__':
    # getscreencap('abc.jpg','D:/screen/')
    # get_screencap_by_hardware('D:/screen/')
    # get_screencap_by_system('abc.jpg', 'D:/screen/')
    # get_screencap_by_system('abc123.jpg', 'D:/screen/')
    # get_screencap_by_system('abc222.jpeg', 'D:/screen/')
    p = subprocess.Popen("adb shell ", shell=True, stdin = subprocess.PIPE,stdout=subprocess.PIPE)
    # p.stdin.write("cd /data/local/tmp")
    # p.stdin.write("ls")
    # p.stdin.write("ls")
    r= p.communicate("cd /data/local/tmp\nls\nexit\nexit\nexit\n")
    print(len(r))
    print(r[0])
    print(type(r[0]))
    # t="a"
    # t1= t.split('&')
    # t2="\n".join(t1)
    # print(str(t2))
    # for i in r:
    #     print i
    #     print(type(i))
    pass